﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Security.AzMan;
using System.Security.Principal;
using Microsoft.Practices.Unity;
using MyInterfaces;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
using MyProxies;
using System.Threading;
using Common.Logging;


namespace MyServiceConsoleApp
{
    class Program
    {
        private static ILog log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
        {
            log.Debug("Main start..");
            IUnityContainer unityContainer = new UnityContainer();
            UnityConfigurationSection section =
                (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            section.Containers["myContainer"].Configure(unityContainer);


            try
            {
                AzManAuthorizationProvider azProvider =
                    new AzManAuthorizationProvider(@"msxml://C:\Users\fritte\Net\TestStore.xml",
                    "TestApp", "", "");

                IPrincipal authorizable = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                // IPrincipal authorizable = (IPrincipal)WindowsIdentity.GetCurrent();
                log.Info(string.Format("User {0} Authorized : {1}",
                                    authorizable.Identity.Name,
                                    azProvider.Authorize(authorizable, "TestRole")));

                //log.Info(string.Format("User {0} IsInTestRole2 : {1}",
                //                    authorizable.Identity.Name,
                //                    azProvider.Authorize(authorizable, "TestRole2")));
                Thread.Sleep(100);
                CBDCommandType commandType = new CBDCommandType();
                commandType.CbdMessage = "Wow .. det här funkar ju riktigt bra.. ";
                commandType.Command = "SPARA";


                /*  Dynamic proxy creation based on Interface  */
                IMyService proxy = new ChannelFactory<IMyService>("MyService").CreateChannel();

                log.Info(string.Format("Result from WCF Sercvice : {0}",
                                                proxy.SayHello("Hello WCF Service")));

                log.Info(string.Format("Result from WCF Service IsThisRight : {0}",
                                                proxy.IsThisRight("left")));

                CBDCommandType response = proxy.UpdateInfo(commandType);
                log.Info(string.Format("Result from WCF Service UpdateInfo : Command:{0} + CbdMessage:{1} ",
                                                response.Command,
                                                response.CbdMessage));

                ((IChannel)proxy).Close();


                /*  Reusable proxy creation with injection from Unity  */
                using (IMyService service = unityContainer.Resolve<IMyService>())
                {
                    log.Info(string.Format("Result from calling MyProxy.SayHello :  {0}",
                                    service.SayHello("Hello proxy..")));

                    CBDCommandType commandType2 = new CBDCommandType();
                    commandType2.CbdMessage = "HELP";
                    commandType2.Command = "oppna";
                    log.Info(string.Format("Result from calling EchoMessage : {0}",
                                    service.EchoMessage(commandType2)));
                }
                log.Error("Från netSend appender ... pretty cool stuff");
            }
            catch(Exception ex)
            {
                log.Error("Nånting dåligt hände: ", ex);
            }
            Console.ReadLine();
            log.Debug("Main and application ends..");
        }
    }
}
